package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.pojo.User;
import com.itheima.reggie.service.SendMailService;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.util.R;
import com.itheima.reggie.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private SendMailService sendMailService;

    @PostMapping("/sendmail")
    public R<String> sendMail(@RequestBody User user, HttpSession session) throws MessagingException {
        //获取手机号
        String mail = user.getMail();

        if(!StringUtils.isEmpty(mail)){

            //生成随机的4位验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();

            //调用邮件服务发送验证邮件
            //sendMailService.sendMail(code,mail);

            System.out.println(code);

            //将生成的验证码存起起来
            //session.setAttribute(mail,code);
            //将生成的验证码保存在redis中
            redisTemplate.opsForValue().set(mail,code,5, TimeUnit.MINUTES);
            return R.success("验证码发送成功");
        }
        return R.error("短信发送失败");
    }

    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session) throws MessagingException {
        //获取手机号
        String mail = map.get("mail").toString();

        //获取验证码
        String code= map.get("code").toString();

        //从session中获取验证码
        //Object codeInSession = session.getAttribute(mail);
        //从redis中获取缓存的验证码
        Object codeInSession = redisTemplate.opsForValue().get(mail);

        //如果比对成功，说明登陆成功
        if(codeInSession != null && codeInSession.equals(code)){
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getMail,mail);

            User user = userService.getOne(lambdaQueryWrapper);

            //判断该邮箱号是否为新用户，如果是就自动注册
            if(user == null){
                user = new User();
                user.setMail(mail);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user",user.getId());

            //如果用户登陆成功，删除redis中缓存的验证码
            redisTemplate.delete(mail);

            return R.success(user);
        }
        return R.error("登陆失败");
    }

    @PostMapping("/loginout")
    public R<String> loginOut(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }
}
